﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using ERP_RORZE_SYSTEM;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_CheckFinished_F4Plan : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        private DataSet ds;
        private DataViewManager dsView;

        private DataTable resultTable;

        public frm_CheckFinished_F4Plan()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.ds = new DataSet("F4Plan");
            LoadData();   

            this.finishedDate.EditValue = DateTime.Now.Date;
            this.rohs.Checked = false;
            this.orderNo.SelectedIndexChanged += orderNo_SelectedIndexChanged;
        }

        void orderNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDataUnFinished();
        }

        ///////////////////////////////////////
        /// Load Database to Binding
        ///////////////////////////////////////
        private void LoadData()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT t1.TimePlanNo FROM [231_TimePlan] AS t1 WHERE (t1.FinishedDate IS NULL)";                
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "F4PlanTable");
                da1.Fill(ds);
                dsView = ds.DefaultViewManager;
                cmd_sql.Dispose();
                myConnection.Close();

                this.orderNo.DataSource = dsView;
                this.orderNo.DisplayMember = "F4PlanTable.TimePlanNo";
                this.orderNo.ValueMember = "F4PlanTable.TimePlanNo";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadDataUnFinished()
        {
            try
            {
                SQLManagement mySQLManagement = new SQLManagement();
                mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
                string cmd_sql = "SELECT t1.OrderNo, t1.CompID, t1.SerialNo, t1.Finished, t1.FinishedDate,t1.RoHS"
                    + " FROM [231_RobotOrderDetail] AS t1 "
                    + " WHERE (t1.FinishedDate IS NULL) AND t1.OrderNo='" + orderNo.Text + "'";

                DataSet sourceDataSet = new DataSet();
                sourceDataSet = mySQLManagement.SQL_Query(cmd_sql);
                if (sourceDataSet != null)
                {
                    gridFinishedReport.DataSource = sourceDataSet.Tables[0];
                }
                else
                {
                    this.resultTable = new DataTable();
                    this.resultTable.Columns.Add("OrderNo", typeof(string));
                    this.resultTable.Columns.Add("CompID", typeof(string));
                    this.resultTable.Columns.Add("SerialNo", typeof(int));
                    this.resultTable.Columns.Add("Finished", typeof(bool));
                    this.resultTable.Columns.Add("FinishedDate", typeof(DateTime));
                    this.resultTable.Columns.Add("RoHS", typeof(bool));
                    this.gridFinishedReport.DataSource = this.resultTable;
                }
                FinishedReport.Columns[0].Caption = "Order No.";
                FinishedReport.Columns[1].Caption = "Comp. ID";
                FinishedReport.Columns[2].Caption = "Serial No.";
                FinishedReport.Columns[3].Caption = "Finished";
                FinishedReport.Columns[4].Caption = "Finished Date";
                FinishedReport.Columns[5].Caption = "RoHS";
                FinishedReport.BestFitColumns();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {

            myConnection.Open();
            SqlTransaction tran = myConnection.BeginTransaction();
            SqlCommand sql_cmd;
            try
            {
                if (FinishedReport.RowCount > 0)
                {
                    for (int i = 0; i < FinishedReport.DataRowCount; i++)
                    {
                        sql_cmd = myConnection.CreateCommand();
                        sql_cmd.CommandText = "UPDATE [231_RobotOrderDetail] SET FinishedDate=@finisheddate,"
                            + " Finished=1, RoHS='" + bool.Parse(rohs.Checked.ToString()) +"'"
                            + " WHERE SerialNo='" + FinishedReport.GetRowCellValue(i, "SerialNo").ToString() + "'";
                        sql_cmd.Parameters.Add("@finisheddate",SqlDbType.DateTime).Value = finishedDate.EditValue;
                        sql_cmd.Transaction = tran;
                        sql_cmd.ExecuteNonQuery();
                        sql_cmd.Dispose();
                    }
                    sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "UPDATE [231_TimePlan] SET FinishedDate=@finisheddate"
                        + " WHERE TimePlanNo='" + orderNo.Text + "'";
                    sql_cmd.Parameters.Add("@finisheddate",SqlDbType.DateTime).Value = finishedDate.EditValue;
                    sql_cmd.Transaction = tran;
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    tran.Commit();
                    myConnection.Close();
                }
                MessageBox.Show("Report has been sent");
                clearForm();
                orderNo.Focus();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        private void FinishedReport_RowCountChanged(object sender, EventArgs e)
        {
            if (FinishedReport.RowCount > 0)
            {
                buttonSend.Enabled = true;
            }
            else
            {
                buttonSend.Enabled = false;
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c.Name != "finishedDate")
                {
                    if (c is System.Windows.Forms.ComboBox)
                    {
                        ((System.Windows.Forms.ComboBox)c).SelectedIndex = -1;
                    }

                    if (c is System.Windows.Forms.TextBox)
                    {
                        ((System.Windows.Forms.TextBox)c).Text = null;
                    }

                    if (c is DevExpress.XtraEditors.TextEdit)
                    {
                        ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                    }

                    if (c is DevExpress.XtraEditors.ComboBoxEdit)
                    {
                        ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                    }

                    if (c is System.Windows.Forms.CheckBox)
                    {
                        ((System.Windows.Forms.CheckBox)c).Checked = false;
                    }

                    if (c is System.Windows.Forms.MaskedTextBox)
                    {
                        ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                    }
                }
            }
        }
    }
}
